package com.ruoyi.distributionConfig.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.distributionConfig.domain.DistributionConfig;
import com.ruoyi.distributionConfig.service.IDistributionConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分销全局配置Controller
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@RestController
@RequestMapping("/ruoyi/distributionConfig")
public class DistributionConfigController extends BaseController
{
    @Autowired
    private IDistributionConfigService distributionConfigService;

    /**
     * 获取分销协议内容（无需权限）
     */
    @GetMapping("/protocol")
    public AjaxResult getApplyProtocol()
    {
        // 获取分销配置（不考虑状态，只考虑是否开启）
        DistributionConfig config = new DistributionConfig();
        config.setDistributionOpen("1"); // 只查询开启的配置
        List<DistributionConfig> list = distributionConfigService.selectDistributionConfigList(config);

        if (!list.isEmpty()) {
            // 返回开启状态下配置的协议内容
            return success(list.get(0).getApplyProtocol());
        } else {
            return AjaxResult.error("分销功能未开启");
        }
    }


    /**
     * 查询分销全局配置列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:distributionConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionConfig distributionConfig)
    {
        startPage();
        List<DistributionConfig> list = distributionConfigService.selectDistributionConfigList(distributionConfig);
        return getDataTable(list);
    }

    /**
     * 导出分销全局配置列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:distributionConfig:export')")
    @Log(title = "分销全局配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DistributionConfig distributionConfig)
    {
        List<DistributionConfig> list = distributionConfigService.selectDistributionConfigList(distributionConfig);
        ExcelUtil<DistributionConfig> util = new ExcelUtil<DistributionConfig>(DistributionConfig.class);
        util.exportExcel(response, list, "分销全局配置数据");
    }

    /**
     * 获取分销全局配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:distributionConfig:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return success(distributionConfigService.selectDistributionConfigByConfigId(configId));
    }

    /**
     * 新增分销全局配置
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:distributionConfig:add')")
    @Log(title = "分销全局配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DistributionConfig distributionConfig)
    {
        return toAjax(distributionConfigService.insertDistributionConfig(distributionConfig));
    }

    /**
     * 修改分销全局配置
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:distributionConfig:edit')")
    @Log(title = "分销全局配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionConfig distributionConfig)
    {
        return toAjax(distributionConfigService.updateDistributionConfig(distributionConfig));
    }

    /**
     * 删除分销全局配置
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:distributionConfig:remove')")
    @Log(title = "分销全局配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(distributionConfigService.deleteDistributionConfigByConfigIds(configIds));
    }
}
